package com.apex.aolonfitdemo.ui;

import android.app.Activity;
import android.app.LocaleManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityAddAlarmBinding;
import com.apex.aolonfitdemo.dialog.MonthDialog;
import com.apex.aolonfitdemo.dialog.NotDisturbDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.EditAttentionCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.CommonAction;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.model.EABleRemindRespond;
import com.apex.ax_bluetooth.model.EABleReminder;
import com.cyber.bridge.module.bean.AlarmEntity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class AddAlarmActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;
    ActivityAddAlarmBinding addAlarmBinding;
    private final int CYCLE_CODE = 0xFF;
    private NotDisturbDialog timeDialog;
    private MonthDialog sleepDialog;
    private int alarmTimeHour, alarmTimeMinute, cycleTime;
    ResultBroadcast resultBroadcast;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                finish();
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(AddAlarmActivity.this, getString(R.string.add_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addAlarmBinding = ActivityAddAlarmBinding.inflate(getLayoutInflater());
        setContentView(addAlarmBinding.getRoot());
        resultBroadcast = new ResultBroadcast();
        registerReceiver(resultBroadcast, new IntentFilter("add.aolonfit.alarmclock"));
        addAlarmBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(addAlarmBinding.tool);
        getSupportActionBar().setTitle(null);
        addAlarmBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        addAlarmBinding.cycleTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(AddAlarmActivity.this, CycleActivity.class), CYCLE_CODE);
            }
        });
        addAlarmBinding.alarmTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (timeDialog == null) {
                    timeDialog = new NotDisturbDialog(AddAlarmActivity.this, getString(R.string.alarm_clock));
                    timeDialog.setSelectListener(new NotDisturbDialog.SelectListener() {
                        @Override
                        public void selectData(int hour, int minute) {
                            addAlarmBinding.alarmTime.setText(hour + ":" + minute);
                            alarmTimeHour = hour;
                            alarmTimeMinute = minute;
                        }
                    });
                }
                if (!timeDialog.isShowing()) {
                    timeDialog.show();
                }
            }
        });
        addAlarmBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(AddAlarmActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    AlarmEntity alarmEntity = new AlarmEntity();
                    alarmEntity.setEnabled(addAlarmBinding.alarmSwitch.isChecked());
                    alarmEntity.setMinute(alarmTimeMinute);
                    alarmEntity.setHour(alarmTimeHour);
                    int[] cTime = new int[7];
                    if ((cycleTime & 0x01) == 0x01) {
                        cTime[0] = 1;
                    } else if ((cycleTime & 0x02) == 0x02) {
                        cTime[1] = 1;
                    } else if ((cycleTime & 0x04) == 0x04) {
                        cTime[2] = 1;
                    } else if ((cycleTime & 0x08) == 0x08) {
                        cTime[3] = 1;
                    } else if ((cycleTime & 0x10) == 0x10) {
                        cTime[4] = 1;
                    } else if ((cycleTime & 0x20) == 0x20) {
                        cTime[5] = 1;
                    } else if ((cycleTime & 0x40) == 0x40) {
                        cTime[6] = 1;
                    }
                    alarmEntity.setRepeat(cTime);
                    AolonFitManager.getInstance().addAlarm(alarmEntity);

            }
        });

    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (resultBroadcast != null) {
            unregisterReceiver(resultBroadcast);
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (timeDialog != null) {
            timeDialog.dismiss();
            timeDialog.destroyDialog();
            timeDialog = null;
        }
        if (sleepDialog != null) {
            sleepDialog.dismiss();
            sleepDialog.destroyDialog();
            sleepDialog = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_CANCELED && requestCode == CYCLE_CODE) {
            cycleTime = data.getIntExtra("circle", 0);
            if (cycleTime == 0x7F) {
                addAlarmBinding.cycleTime.setText(getText(R.string.every_day));

            } else {
                addAlarmBinding.cycleTime.setText(data.getStringExtra("circleTime"));
            }


        }
    }

    class ResultBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action) && action.equalsIgnoreCase("add.aolonfit.alarmclock")) {
                boolean result = intent.getBooleanExtra("result", false);
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(result ? 0x40 : 0x41);
                }
            }
        }
    }
}
